package com.material.controller.supplier;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.material.common.annotation.LoginUser;
import com.material.common.base.CommonController;
import com.material.common.core.domain.entity.SysUser;
import com.material.common.entity.ResultResponse;
import com.material.common.utils.bean.BeanUtil;
import com.material.request.SupplierCarAddRequest;
import com.material.request.SupplierCarQueryRequest;
import com.material.supplier.domain.SupplierCar;
import com.material.supplier.service.ISupplierCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "3.供应商车辆管理")
@RestController
@RequestMapping("/supplier/car")
public class SupplierCarController extends CommonController {

    @Autowired
    private ISupplierCarService supplierCarService;

    @ApiOperation("车辆列表")
    @GetMapping("/list")
    public ResultResponse carList(SupplierCarQueryRequest supplierCarQueryRequest){
        PageHelper.startPage(supplierCarQueryRequest.getPageNum(),supplierCarQueryRequest.getPageSize());
        SupplierCar query = new SupplierCar();
        query.setCarNum(supplierCarQueryRequest.getCarNum());
        List<SupplierCar> supplierCars = supplierCarService.selectSupplierCarList(query);
        PageInfo<SupplierCar> pageInfo = new PageInfo<>(supplierCars);
        return ResultResponse.success(pageInfo);
    }

    @ApiOperation("添加车辆")
    @PostMapping("/add")
    public ResultResponse addCar(@ApiIgnore @LoginUser SysUser sysUser, @RequestBody SupplierCarAddRequest supplierCarAddRequest){
        checkSupplierUser(sysUser);
        SupplierCar supplierCar = BeanUtil.copyProperties(supplierCarAddRequest, SupplierCar.class);
        supplierCar.setSupplierId(sysUser.getBizId());
        SupplierCar exist = supplierCarService.selectSupplierCarByCarNum(supplierCarAddRequest.getCarNum(), sysUser.getBizId());
        if(exist != null){
            return ResultResponse.error("车辆已经存在，请勿重复添加");
        }
        supplierCarService.insertSupplierCar(supplierCar);
        return ResultResponse.success();
    }
}
